package org.igeek.config.datasource.ch01;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @package org.igeek.config.datasource.ch01
 * @Description 数据源的配置类
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/11/14 11:23
 */
//默认的类路径下  加载配置文件db.properties
@PropertySource("db.properties")
public class DataSourceConfig {
    //无法使用@Component注解  无法修改源代码

    @Bean
    public DataSource dataSource(
            @Value("${db.user}") String user,
            @Value("${db.password}") String password,
            @Value("${db.driverClass}") String driverClass,
            @Value("${db.url}") String url
    ) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setDriverClass(driverClass);
        dataSource.setJdbcUrl(url);
        return dataSource;

    }
}
